<?php

/*
 * 依设定日期跟新门店和公司
 */

$serverFilePath = realpath(dirname(__FILE__));;//realpath(dirname(__FILE__)); //$_SERVER["SCRIPT_FILENAME"];
$doc_dir = strrpos($serverFilePath, '/crontab');
$doc_dir = substr($serverFilePath, 0, $doc_dir + 1);   //project root
include_once $doc_dir . '/config/lib/Config.php';
include_once $doc_dir . '/plugin/Logging.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'elm_sdk'.DIRECTORY_SEPARATOR.'Config';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'elm_sdk'.DIRECTORY_SEPARATOR.'Api';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'elm_sdk'.DIRECTORY_SEPARATOR.'Exception';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'elm_sdk'.DIRECTORY_SEPARATOR.'OAuth';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'elm_sdk'.DIRECTORY_SEPARATOR.'Protocol';


set_time_limit(0);
error_reporting(E_ALL);
header("Content-type:text/html;charset:utf-8");

$config_ini = $doc_dir . 'config/config.ini';

$ini_array = parse_ini_file($config_ini, true);
$db_array = $ini_array['DB'];
$getWebsite = $ini_array['WebSite'];
$con = mysqli_init();

mysqli_real_connect($con, $db_array['host'], $db_array['id'], $db_array['pwd'], $db_array['database'], $db_array['port']);
mysqli_set_charset($con, $db_array['encoding']);

$t = date('H');
if($t == 23){
    $sql_d = "delete from elm  where to_days(now())-to_days(push_time)>=3";
    mysqli_query($con, $sql_d);
}

$sql = "select * from elm where status = 1";
$result_elm = mysqli_query($con, $sql);
$elm = [];
while ($row = mysqli_fetch_array($result_elm, MYSQLI_ASSOC)) {
    $elm[] = $row;
}

function send_post($url, $post_data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = curl_exec($ch);
    $aStatus = curl_getinfo($ch); # 请求头信息
    curl_close($ch);
    return $output;
}

foreach ($elm as $k => $val) {
    $u = $getWebsite['website_url'].'?md=service&cl=ELM&at=getOrders_crontab';
    $d['orderID'] = $val['orderID'];
    $r = send_post($u,$d);
    $r = json_decode($r,true);
    if ($r && $r['s'] == 1) {

        echo $val['orderID'].'添加成功';
    }else{
        echo $val['orderID'].'添加失败';
    }
}

echo ('....................................');

function insertValue($arr) {
    $keys = array();
    $values = array();

    foreach ($arr as $k => $val) {
        $keys[] = "`" . $k . "`";
        $values[] = "'" . $val . "'";
    }
    $ks = implode(",", $keys);
    $vals = implode(",", $values);
    $re = "(" . $ks . ", createdt) values (" . $vals . ", now())";

    return $re;
}

function updateValue($arr) {
    $result = array();
    foreach ($arr as $k => $val) {
        $result[] = "`" . $k . "`" . "='" . $val . "'";
    }
    $result[] = "`updatedt`" . "=now() ";

    $re = implode(",", $result);
    return $re;
}

function mysql_escape($content) {
    if (!get_magic_quotes_gpc()) {

        if (is_array($content)) {
            foreach ($content as $key => $value) {
                $content[$key] = addslashes($value);
            }
        } else {
            $content = addslashes($content);
        }
    }
    return $content;
}


/**
 * 求两个已知经纬度之间的距离,单位为米
 * @param lng1 $ ,lng2 经度
 * @param lat1 $ ,lat2 纬度
 * @return float 距离，单位米
 */
function getdistanceAction($lng1, $lat1, $lng2, $lat2)
{
    $radLat1 = deg2rad($lat1);
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
    return $s;
}
?>

